using System;
using System.Windows.Forms;

using DirkRettschlag.Utils;
using System.Collections.Generic;

namespace Departure
{
    public partial class frmSettings : Form
    {
        #region Variables

        GeneralSettings generalSettings;

        #endregion Variables

        #region Properties

        public GeneralSettings GeneralSettings
        {
            get { return this.generalSettings; }
            set { this.generalSettings = value; }
        }

        #endregion Properties

        #region Constructor

        public frmSettings(Settings settings)
        {
            InitializeComponent();

            if (Platform.GetDeviceType() != "Smartphone")
            {
                InitializeComponentPPC();
            }

            this.generalSettings = new GeneralSettings(settings);

            //this.labelCountdownTrainFormatDesc.Text = "Description:\r\n\r\n%TI%\t- Time\r\n%TR%\t- Train\r\n%DI%\t- Direction\r\n%PL%\t- Platform";

            this.generalSettings.getSettings();

            this.generalSettings.saveSettings(); // workaround for first time run

            this.loadSkins();         

            this.setControls();

            buttonMain_Click(this, EventArgs.Empty);

        }

        #endregion Constructor

        #region Public Methods

        public void SetStationOrderListView(List<string> order, SortedList<string, ctrlStationListItem> stationItems)
        {
            listViewStationOrder.Items.Clear();

            foreach (string key in order)
            {
                listViewStationOrder.Items.Add(new ListViewItem(new string[] { stationItems[key].StationName, key }));
            }

        }

        public List<string> GetStationOrderListView()
        {
            List<string> order = new List<string>();

            foreach (ListViewItem item in listViewStationOrder.Items)
            {
                order.Add(item.SubItems[1].Text);
            }

            return order;
        }

        #endregion Public Methods

        #region Private Methods

        private void setControls()
        {
            this.chkAutoUpdate.Checked = this.generalSettings.AutoUpdate;
            this.numericUpDownUpdateStations.Value = (decimal)this.generalSettings.UpdateInterval;
            this.chkWebService.Checked = this.generalSettings.UseWebService;
            this.chkAutoConnect.Checked = this.generalSettings.AutoConnect;
            this.textBoxHttpHeader.Text = this.generalSettings.HTTPHeader;
            this.textBoxUserAgent.Text = this.generalSettings.UserAgent;
            this.chkRemovePastDepartures.Checked = this.generalSettings.RemovePastDepartures;
            this.textBoxCountdownTrainFormat.Text = this.generalSettings.CountdownTrainFormat;
            this.textBoxWebServiceURL.Text = this.generalSettings.WebServiceURL;
            if (this.generalSettings.Skin == "")
            {
                this.comboBoxSkin.SelectedIndex = 0;
                this.generalSettings.Skin = (string)comboBoxSkin.SelectedItem;
            }
            else
                this.comboBoxSkin.SelectedItem = this.generalSettings.Skin;
            this.checkBoxSortAlphabetically.Checked = this.generalSettings.SortAlphabetically;
        }

        private void setGeneralSettings()
        {
            this.generalSettings.AutoUpdate = chkAutoUpdate.Checked;
            this.generalSettings.UpdateInterval = (double)numericUpDownUpdateStations.Value;
            this.generalSettings.UseWebService = chkWebService.Checked;
            this.generalSettings.AutoConnect = chkAutoConnect.Checked;
            this.generalSettings.HTTPHeader = textBoxHttpHeader.Text;
            this.generalSettings.UserAgent = textBoxUserAgent.Text;
            this.generalSettings.RemovePastDepartures = chkRemovePastDepartures.Checked;
            this.generalSettings.CountdownTrainFormat = textBoxCountdownTrainFormat.Text;
            this.generalSettings.WebServiceURL = textBoxWebServiceURL.Text;
            this.generalSettings.Skin = (string)comboBoxSkin.SelectedItem;
            this.generalSettings.SortAlphabetically = this.checkBoxSortAlphabetically.Checked;
        }

        private void mniSave_Click(object sender, EventArgs e)
        {
            this.setGeneralSettings();
            this.generalSettings.saveSettings();
            this.DialogResult = DialogResult.OK;
            this.Close();
        }

        private void mniCancel_Click(object sender, EventArgs e)
        {
            this.generalSettings.getSettings();
            this.setControls();
            this.DialogResult = DialogResult.Cancel;
            this.Close();
        }

        private void inputPanel_EnabledChanged(object sender, EventArgs e)
        {
            if (this.inputPanel.Enabled)
            {
                this.panelSettings.Height -= this.inputPanel.Bounds.Height;
            }
            else
            {
                this.panelSettings.Height = this.ClientSize.Height;
            }
        }

        private void loadSkins()
        {
            System.IO.DirectoryInfo dir = new System.IO.DirectoryInfo(DirkRettschlag.Utils.Utilities.AppPath() + "\\skins");

            foreach (System.IO.FileInfo file in dir.GetFiles("*.skin"))
            {
                if (file.Name != ".skin")
                    comboBoxSkin.Items.Add(file.Name.Replace(".skin", ""));
            }
        }

        #endregion Private Methods

        private void buttonMain_Click(object sender, EventArgs e)
        {
            panelSettingsMain.Visible = true;
            panelSettingsUI.Visible = false;
            panelSettingsOrder.Visible = false;
        }

        private void buttonUI_Click(object sender, EventArgs e)
        {
            panelSettingsUI.Visible = true;
            panelSettingsMain.Visible = false;
            panelSettingsOrder.Visible = false;
        }

        private void buttonOrder_Click(object sender, EventArgs e)
        {
            panelSettingsOrder.Visible = true;
            panelSettingsUI.Visible = false;
            panelSettingsMain.Visible = false;
        }

        private void checkBoxSortAlphabetically_CheckStateChanged(object sender, EventArgs e)
        {
            panelOrder.Enabled = !checkBoxSortAlphabetically.Checked;
        }

        private void frmSettings_Resize(object sender, EventArgs e)
        {
            this.listViewStationOrder.Columns[0].Width = this.listViewStationOrder.Width - (this.CurrentAutoScaleDimensions.Height == 192 ? 4 : 2);
        }

        private void MoveListViewItem(ref ListView lv, bool moveUp)
        {
            string cache;
            int selIdx;

            selIdx = lv.SelectedIndices[0];
            if (moveUp)
            {
                // ignore moveup of row(0)
                if (selIdx == 0)
                    return;

                // move the subitems for the previous row
                // to cache to make room for the selected row
                for (int i = 0; i < lv.Items[selIdx].SubItems.Count; i++)
                {
                    cache = lv.Items[selIdx - 1].SubItems[i].Text;
                    lv.Items[selIdx - 1].SubItems[i].Text =
                      lv.Items[selIdx].SubItems[i].Text;
                    lv.Items[selIdx].SubItems[i].Text = cache;
                }
                lv.Items[selIdx - 1].Selected = true;
            }
            else
            {
                // ignore movedown of last item
                if (selIdx == lv.Items.Count - 1)
                    return;
                // move the subitems for the next row
                // to cache so we can move the selected row down
                for (int i = 0; i < lv.Items[selIdx].SubItems.Count; i++)
                {
                    cache = lv.Items[selIdx + 1].SubItems[i].Text;
                    lv.Items[selIdx + 1].SubItems[i].Text =
                      lv.Items[selIdx].SubItems[i].Text;
                    lv.Items[selIdx].SubItems[i].Text = cache;
                }
                lv.Items[selIdx + 1].Selected = true;
            }

            //lv.Focus();
            lv.Invalidate();
        }

        private void buttonUp_Click(object sender, EventArgs e)
        {
            MoveListViewItem(ref listViewStationOrder, true);
        }

        private void buttonDown_Click(object sender, EventArgs e)
        {
            MoveListViewItem(ref listViewStationOrder, false);
        }

        private void frmSettings_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            
        }
       
    }
}
